package shop.xmall.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.xmall.framework.core.controller.BaseController;
import shop.xmall.framework.core.domain.ResponseResultDto;
import shop.xmall.framework.core.page.PageDto;
import shop.xmall.system.domain.SysRole;
import shop.xmall.system.service.ISysRoleService;

import java.util.List;

/**
 * 角色信息Controller
 *
 * @author xmall
 * @date 2019-10-09
 */
@Controller
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询角色信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public ResponseResultDto list(SysRole sysRole) {
        PageHelper.offsetPage(getPageNum() - 1, getPageSize());
        List<SysRole> list = sysRoleService.selectSysRoleList(sysRole);
        PageInfo pageInfo = new PageInfo(list);
        PageDto pageDto = new PageDto(list, pageInfo.getTotal(), getPageNum(), getPageSize());
        return ResponseResultDto.success(pageDto);
    }


    /**
     * 查询角色信息
     */
    @PostMapping("/getById")
    @ResponseBody
    public ResponseResultDto getById(Long roleId) {
        return ResponseResultDto.success(sysRoleService.selectSysRoleById(roleId));
    }


    /**
     * 新增保存角色信息
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseResultDto addSave(SysRole sysRole) {
        return toAjax(sysRoleService.insertSysRole(sysRole));
    }


    /**
     * 修改保存角色信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResponseResultDto editSave(SysRole sysRole) {
        return toAjax(sysRoleService.updateSysRole(sysRole));
    }

    /**
     * 删除角色信息
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResponseResultDto remove(String ids) {
        return toAjax(sysRoleService.deleteSysRoleByIds(ids));
    }


    /**
     * 根据用户id查询角色信息
     */
    @PostMapping("/listByUserId")
    @ResponseBody
    public ResponseResultDto listByUserId(Long userId) {
        return ResponseResultDto.success(sysRoleService.listByUserId(userId));
    }

    /**
     * 判断是否存在角色名称、权限字符等
     */
    @PostMapping("/isExist")
    @ResponseBody
    public ResponseResultDto isExist(SysRole sysRole) {
        int count = sysRoleService.isExist(sysRole);
        return toAjax(count);
    }
}